package com.example.module_main.main

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.Router
import com.example.lib_common.base.BaseActivity
import com.example.module_main.R
import com.example.module_main.databinding.ActivityMainBinding
import com.example.module_main.dialog.AppUpdateDialog
import dagger.hilt.android.AndroidEntryPoint

/***
 * 1.判断版本是否需要更新
 */
@AndroidEntryPoint
@Route(path = Router.MAIN_MAIN_ACTIVITY)
class MainActivity : BaseActivity<MainViewModel,ActivityMainBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_main
    override val mViewModel: MainViewModel by viewModels()

    companion object {
        private const val CLUB_CLUB_FRAGMENT = "club"
        private const val MOTOR_MOTOR_FRAGMENT = "motor"
        private const val MINE_MINE_FRAGMENT = "mine"
        private const val MALL_MALL_FRAGMENT = "mall"
        private const val MESSAGE_MESSAGE_FRAGMENT = "message"
    }


    private val mClubFragment by lazy{
        ARouter.getInstance().build(Router.CLUB_CLUB_FRAGMENT).navigation() as Fragment
    }
    private val mMotorCircleFragment by lazy{
        ARouter.getInstance().build(Router.MOTOR_MOTOR_FRAGMENT).navigation() as Fragment
    }
    private val mMineFragment by lazy{
        ARouter.getInstance().build(Router.MINE_MINE_FRAGMENT).navigation() as Fragment
    }
    private val mMallFragment by lazy{
        ARouter.getInstance().build(Router.MALL_MALL_FRAGMENT).navigation() as Fragment
    }
    private val mMessageFragment by lazy{
        ARouter.getInstance().build(Router.MESSAGE_MESSAGE_FRAGMENT).navigation() as Fragment
    }

    /**
     * 切换fragment
     * 1.开启事务
     * 2.操作：hide show add replace
     * 3.提交事务
     */
    private fun naToFragment(id:Int){
        val bt = supportFragmentManager.beginTransaction()//开启事务
        hideCurrentFragment(bt) //隐藏正在显示的fragment
        when(id){
            R.id.nav_motor_club ->{
                if(supportFragmentManager.findFragmentByTag(CLUB_CLUB_FRAGMENT)?.isAdded == true){
                    bt.show(mClubFragment)
                }else{
                    bt.add(R.id.fragment_container,mClubFragment,CLUB_CLUB_FRAGMENT)
                }
                bt.commitNowAllowingStateLoss() //提交事务
            }

            R.id.nav_message ->{
                if(supportFragmentManager.findFragmentByTag(MESSAGE_MESSAGE_FRAGMENT)?.isAdded == true){
                    bt.show(mMessageFragment)
                }else{
                    bt.add(R.id.fragment_container,mMessageFragment, MESSAGE_MESSAGE_FRAGMENT)
                }
                bt.commitNowAllowingStateLoss() //提交事务
            }

            R.id.nav_mine ->{
                if(supportFragmentManager.findFragmentByTag(MINE_MINE_FRAGMENT)?.isAdded == true){
                    bt.show(mMineFragment)
                }else{
                    bt.add(R.id.fragment_container,mMineFragment,MINE_MINE_FRAGMENT)
                }
                bt.commitNowAllowingStateLoss() //提交事务
            }

            R.id.nav_motor_mall ->{
                if(supportFragmentManager.findFragmentByTag(MALL_MALL_FRAGMENT)?.isAdded == true){
                    bt.show(mMallFragment)
                }else{
                    bt.add(R.id.fragment_container,mMallFragment,MALL_MALL_FRAGMENT)
                }
                bt.commitNowAllowingStateLoss() //提交事务
            }

            R.id.nav_motor_circle ->{
                if(supportFragmentManager.findFragmentByTag(MOTOR_MOTOR_FRAGMENT)?.isAdded == true){
                    bt.show(mMotorCircleFragment)
                }else{
                    bt.add(R.id.fragment_container,mMotorCircleFragment,MOTOR_MOTOR_FRAGMENT)
                }
                bt.commitNowAllowingStateLoss() //提交事务
            }
        }

    }
    private fun hideCurrentFragment(bt:FragmentTransaction){
        if(supportFragmentManager.findFragmentByTag(MOTOR_MOTOR_FRAGMENT)?.isVisible == true){
            bt.hide(mMotorCircleFragment)
        }
        if(supportFragmentManager.findFragmentByTag(CLUB_CLUB_FRAGMENT)?.isVisible == true){
            bt.hide(mClubFragment)
        }
        if(supportFragmentManager.findFragmentByTag(MALL_MALL_FRAGMENT)?.isVisible == true){
            bt.hide(mMallFragment)
        }
        if(supportFragmentManager.findFragmentByTag(MINE_MINE_FRAGMENT)?.isVisible == true){
            bt.hide(mMineFragment)
        }
        if(supportFragmentManager.findFragmentByTag(MESSAGE_MESSAGE_FRAGMENT)?.isVisible == true){
            bt.hide(mMessageFragment)
        }
    }



    override fun initView() {
        naToFragment(R.id.nav_motor_club)
    }

    override fun initData() {
        mViewModel.appLevelCheck()
    }

    override fun initListener() {
       //版本更新
        mViewModel.mUpdateLiveData.observe(this){updateModel->
//{"code":0,"errorCode":null,"msg":null,"data":{"appLevelId":"11","platform":"ANDROID","latestVersion":"2.2.4","downloadUrl":"https://skl-prod.oss-cn-shenzhen.aliyuncs.com/zs-club-product-release_2_2_4.apk","forceUpGrade":1,"message":"检测到app有新的版本，立即更新！ ","createTime":"2024-09-12 17:46:30"}}
            if(updateModel.forceUpGrade != 2){
                AppUpdateDialog(
                    title = String.format("最新版本%s升级说明",updateModel.latestVersion),
                    message = updateModel.message,
                    force = updateModel.forceUpGrade != 2,
                    onUpdateClick = {
                        Toast.makeText(this,"点击了更新",Toast.LENGTH_SHORT).show()
                    }
                ).show(supportFragmentManager)
            }
        }
        //点击底部导航切换
        mBinding.rgBottom.setOnCheckedChangeListener { group, checkedId ->
            naToFragment(checkedId)
        }
    }


}